<template>
	<div class="login-container">
		<div class="login-box">
			<div class="login-title"></div>
			<!-- 表单区域 -->
			<el-form ref="loginRef" :model="loginInfo" :rules="loginRules">
				<el-form-item prop="username">
					<el-input placeholder="请输入用户名" v-model="loginInfo.username"></el-input>
				</el-form-item>
				<el-form-item prop="password">
					<el-input type="password" placeholder="请输入密码" v-model="loginInfo.password"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="loginBtn" class="btn">登录</el-button>
					<el-link type="info" @click="$router.push('/reg')">去注册</el-link>
				</el-form-item>
			</el-form>
		</div>
	</div>
</template>
<script>
import { loginAPI } from '@/api'
import { mapMutations } from 'vuex'
export default {
  name: 'my-login',
  data () {
    return {
      loginInfo: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [
          { required: true, message: '请填写用户名', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,10}$/, message: '请填写1-10位大小写字母和数字', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请填写密码', trigger: 'blur' },
          { pattern: /^\S{6,15}$/, message: '6-15位非空格字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    ...mapMutations(['updateToken']),
    loginBtn () {
      this.$refs.loginRef.validate(async valid => {
        if (!valid) return false
        const { data: res } = await loginAPI(this.loginInfo)
        // console.log(res)
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success('登录成功')
        this.updateToken(res.token)
        this.$router.push('/home')
      })
    }
  }

}
</script>
<style lang="less" scoped>
.login-container {
	background: url('../../assets/images/login_bg.jpg') center;
	height: 100%;

	.login-box {
		position: absolute;
		left: 50%;
		top: 50%;
		width: 350px;
		height: 280px;
		transform: translate(-50%; -50%);
		background-color: #fff;
		padding: 0 30px;
		border-radius: 5px;

		.login-title {
			background: url('../../assets/images/login_title.png') no-repeat center;
			height: 60px;
		}

		.btn {
			width: 100%;
		}
	}
}
</style>
